package com.inxedu.os.edu.service.system.impl;

import com.inxedu.os.edu.dao.SysRoleFunctionMapper;
import com.inxedu.os.edu.dao.SysRoleMapper;
import com.inxedu.os.edu.pojo.SysRole;
import com.inxedu.os.edu.pojo.SysRoleExample;

import com.inxedu.os.edu.pojo.SysRoleFunction;
import com.inxedu.os.edu.pojo.SysRoleFunctionExample;
import com.inxedu.os.edu.service.system.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleFunctionMapper sysRoleFunctionMapper;

    //查询所有
    @Override
    public List<SysRole> queryAllRoleList() {
        SysRoleExample example=new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeIsNotNull();
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(example);
        return sysRoles;
    }

    //查看Byid
    @Override
    public List<Integer> selectRoleById(Integer roleId) {
        List<Integer> sysRoles = sysRoleMapper.selectRoleById(roleId);
        return sysRoles;
    }


    //添加
    @Override
    public int insertSysRoleByRoleName(SysRole sysRole) {

        sysRole.setCreateTime(new Date());
        int i = sysRoleMapper.insertSysRoleByName(sysRole);

        return i;
    }

    @Override
    public SysRole findSysRoleByRoleName(SysRole sysRole) {
       try {
           SysRoleExample example = new SysRoleExample();
           SysRoleExample.Criteria criteria = example.createCriteria();
           criteria.andRoleNameEqualTo(sysRole.getRoleName());

           List<SysRole> roles = sysRoleMapper.selectByExample(example);
           return roles.get(0);
       }catch (Exception e){

       }
       return null;
    }

    @Override
    public int updateSysRoleName(SysRole sysRole) {

        SysRoleExample sysRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(sysRole.getRoleId());
        int i = sysRoleMapper.updateByExampleSelective(sysRole, sysRoleExample);

        return i;
    }

    /**
     * 保存
     * @param roleId
     * @param functionIds
     * @return
     */
    @Override
    public int saveSysRoleByRoleId(Integer roleId, String functionIds) {
        if(functionIds!=null && functionIds.trim().length()>0){
            if(functionIds.trim().endsWith(",")){
                functionIds = functionIds.trim().substring(0,functionIds.trim().length()-1);
            }
        }

        String[] split = functionIds.split(",");
        List<Integer> list=new ArrayList();
        for (int i=0;i<split.length;i++){
            list.add(Integer.parseInt(split[i]));
        }
        SysRoleFunction function = new SysRoleFunction();
        function.setRoleId(roleId);
        SysRoleFunctionExample example = new SysRoleFunctionExample();
        SysRoleFunctionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        sysRoleFunctionMapper.deleteByExample(example);

        int h=0;
        for (int j=0;j<list.size();j++){

            function.setFunctionId(list.get(j));
            h=sysRoleFunctionMapper.insert(function);

        }


        return h;
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @Override
    public int deleteSysRoleByRoleId(Integer roleId) {
        SysRoleFunctionExample example = new SysRoleFunctionExample();
        SysRoleFunctionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        int i = sysRoleFunctionMapper.deleteByExample(example);
        SysRoleExample roleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria1 = roleExample.createCriteria();
        criteria1.andRoleIdEqualTo(roleId);


        int j = sysRoleMapper.deleteByExample(roleExample);
        if(i>0&&j>0){
           return 1;
        }

        return 0;
    }
}
